package com.dongdong.service.impl;

import com.alibaba.dubbo.config.annotation.Service;
import com.alibaba.fastjson.JSONObject;
import com.dongdong.mapper.TbOrderItemMapper;
import com.dongdong.mapper.TbOrderMapper;
import com.dongdong.pojo.Cart;
import com.dongdong.pojo.TbOrder;
import com.dongdong.pojo.TbOrderItem;
import com.dongdong.service.OrderService;
import com.dongdong.util.IdWorker;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.stereotype.Component;

import javax.annotation.Resource;
import java.math.BigDecimal;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @Deacription TODO
 * @Author Lenovo
 * @Date 2020/12/22 16:17
 * @Version 1.0
 **/
@Service
@Component
public class OrderServiceImpl implements OrderService {

    @Resource
    private TbOrderMapper orderMapper;
    @Resource
    private TbOrderItemMapper orderItemMapper;
    @Resource
    private IdWorker idWorker;
    @Resource
    private RedisTemplate redisTemplate;

    @Override
    public Map<String, String> addOrder(TbOrder order) {
        //得到购物车
        List<Cart> cartList = (List<Cart>)redisTemplate.boundHashOps("cartList").get(order.getUserId());
        long payOrder=idWorker.nextId();
        double totalPrice=0.0;
        Map<String,String> map=new HashMap<>();
        for (Cart cart : cartList) {
            long orderId= idWorker.nextId();
            TbOrder tborder = new TbOrder();//新创建订单对象
            tborder.setOrderId(orderId);//订单Id
            tborder.setUserId(order.getUserId());//用户名
            tborder.setPaymentType(order.getPaymentType());//支付类型
            tborder.setStatus("1");//未付款
            tborder.setCreateTime(new Date());//订单创建日期
            tborder.setUpdateTime(new Date());//订单修改日期
            tborder.setReceiverAreaName(order.getReceiverAreaName());//地址
            tborder.setReceiverMobile(order.getReceiverMobile());//手机号
            tborder.setReceiver(order.getReceiver());//收获人
            tborder.setSourceType("2");//PC端
            tborder.setSellerId(cart.getSellerId());//商家ID

            //循环购物车明细
            double money = 0;
            for (TbOrderItem orderItem : cart.getOrderItemList()) {
                orderItem.setId(idWorker.nextId());
                orderItem.setOrderId(orderId);//订单Id
                orderItem.setSellerId(cart.getSellerId());
                orderItem.setStauts("1");
                money += orderItem.getTotalFee().doubleValue();//金额累加
                orderItemMapper.insertSelective(orderItem);
            }
            tborder.setPayment(new BigDecimal(money));
            totalPrice += money;
            tborder.setPayOrder(payOrder);//母订单
            orderMapper.insertSelective(tborder);
        }
        //将购物车信息和母订单号保存到redis中
        redisTemplate.boundValueOps(order.getUserId()+payOrder).set(JSONObject.toJSONString(cartList));

        //支付
        map.put("payOrder",String.valueOf(payOrder));
        System.out.println("payOrder"+payOrder);
        map.put("totalPrice",String.valueOf(totalPrice));
        System.out.println("totalPrice"+totalPrice);
        return map;
    }

    @Override
    public void updateStatusById(long parseLong) {
        TbOrder order = new TbOrder();
        order.setOrderId(parseLong);
        order.setStatus("6");//交易关闭
        orderMapper.updateByPrimaryKeySelective(order);
    }

    @Override
    public List<TbOrder> getOrderByUserName(String username) {
        return orderMapper.getOrderByUserName(username);
    }

    @Override
    public TbOrder selectOrderByOrderId(Long orderId) {
        return orderMapper.selectByPrimaryKey(orderId);
    }
}

